package com.acc.softmgr.business.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.acc.softmgr.business.model.SysMenu;
import com.acc.softmgr.business.model.SysUser;
import com.acc.softmgr.business.service.ISysMenuService;
import com.acc.softmgr.common.constant.enums.RestStatus;
import com.acc.softmgr.common.web.PageObject;
import com.acc.softmgr.common.web.ResponseObject;

@Controller
public class IndexController {

	@Autowired
	private ISysMenuService sysMenuService;

	/**
	 * 登陆页
	 * 
	 * @return
	 */
	@RequestMapping(value = "/", method = RequestMethod.GET)
	public String login(Model model) {
		return "login/login";
	}

	/**
	 * 首页
	 * 
	 * @return
	 */
	@RequestMapping(value = "index", method = RequestMethod.GET)
	public String index(Model model) {
		return "index/index";
	}

	/**
	 * 首页main页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "main", method = RequestMethod.GET)
	public String main(Model model) {
		return "index/include/main";
	}

	/**
	 * 首页left页面
	 * 
	 * @return
	 */
	@RequestMapping(value = "searchSystemMenus", method = RequestMethod.GET)
	@ResponseBody
	public ResponseObject searchSystemMenus(HttpServletRequest request) {
		ResponseObject responseObject = new ResponseObject();
		SysUser currentUser = (SysUser) request.getSession().getAttribute("currentUser");
		List<SysMenu> menus = sysMenuService.selectMenusByUserId(currentUser.getId());
		List<SysMenu> results = new ArrayList<SysMenu>();
		if (menus != null && menus.size() > 0) {
			results = PageObject.buildTree(menus);
			responseObject.setData(results);
		}
		responseObject.setStatus(RestStatus.SUCCESS.code);
		responseObject.setMessage(RestStatus.SUCCESS.message);
		return responseObject;
	}

	@RequestMapping("/set")
	@ResponseBody
	String set(HttpServletRequest req) {
		req.getSession().setAttribute("testKey", "testValue");
		return "设置session:testKey=testValue";
	}

	@RequestMapping("/query")
	@ResponseBody
	String query(HttpServletRequest req) {
		Object value = req.getSession().getAttribute("testKey");
		return "查询Session：\"testKey\"=" + value;
	}

}
